#include <netdb.h>
#include <sys/socket.h>
#include <stdio.h>

#define STR_HOSTNAME "www.baidu.com"

int main( )
{
    struct hostent *pHostEntry;
    char   acString[32];

	/* get info from hostname */
    pHostEntry = gethostbyname( STR_HOSTNAME );
	if( NULL == pHostEntry )
	{
		printf( "gethostbyname Error! \n" );
		return -1;
	}

	/* anylyze the structure 1*/
    printf("official hostname:%s\n",pHostEntry->h_name);
	
	/* anylyze the structure 2*/
	char **pStrAliase = NULL;
    for( pStrAliase = pHostEntry->h_aliases; *pStrAliase != NULL; pStrAliase++)
	{
        printf(" alias:%s\n",*pStrAliase);
	}

	/* anylyze the structure 3*/
    char **ppAddr = NULL;
    switch(pHostEntry->h_addrtype)
    {
        case AF_INET:
        case AF_INET6:
            ppAddr=pHostEntry->h_addr_list;
            for(; *ppAddr!=NULL; ppAddr++)
			{
				inet_ntop(pHostEntry->h_addrtype, *ppAddr, acString, sizeof(acString));
                printf("address:%s\n", acString	);
			}

			inet_ntop(pHostEntry->h_addrtype, pHostEntry->h_addr, acString, sizeof(acString));
            printf("first address: %s\n", acString );
        	break;
        default:
            printf("unknown address type\n");
        	break;
    }

    return 0;
}
